define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var Form = require("form");
	var scatterCheckInSvc = require("../service/scatterCheckInSvc");
	var processData = require("../store/processData");
	var ScatterCheckInRoomForm = require("../component/ScatterCheckInRoomForm");
	var ScatterCheckInFormEnd = require("../component/ScatterCheckInFormEnd");
	var scatterCheckInBiz = require("../business/scatterCheckInBiz");
	
    module.exports = {
        subnav: {
        	//保存
        	handleSaveClick: function (widget) {
            	var checkInData = {};
            		var form ={};
            		if(widget.get("showForm")=='.J-form'){
            			form = widget.get("form");
            		}else{
            			form =widget.get("doorForm");
            		}
        			editgrid = widget.get("editgrid"),
        			formEnd = widget.get("formEnd"),
        			formData = form.getData(),
        			editgridData = editgrid.getData(),
        			formEndData = formEnd.getData(),
            		roomTypeForm = widget.get("roomTypeFormData"),
            		roomTypeFormData = [],
            		checkInDate = form.getValue("checkInDate"),
            		checkOutDate = form.getValue("checkOutDate"),
            		status = form.getValue("status"),
            		roomStatus = form.getValue("roomStatus");
            		statementStatus = form.getValue("statementStatus"),
            		statusNonMemberGroup = form.getValue("statusNonMemberGroup"),
            		pkNonMemberGroup = form.getValue("pkNonMemberGroup"),
            		versionNonMemberGroup = form.getValue("versionNonMemberGroup"),
            		sourceType = form.getValue("sourceType"),
            		signUpType = form.getValue("signUpType"),            		
            		pkNonMemGroupReserve = form.getValue("pkNonMemGroupReserve"),
            		versionNonMemGroupReserve = form.getValue("versionNonMemGroupReserve"),
            		operator = formEnd.getValue("pkOperator"),
            		operateDate = formEnd.getValue("operateDate"),
            		pledge = Number(formEnd.getValue("pledge")).toFixed(2),
            		signUpRooms = [],
            		tourType = ""; 
            		
            		for(var i=0;i<roomTypeForm.length;i++){
            			roomTypeFormData.push(roomTypeForm[i].getData());
            		}
            		if(signUpType == "Experience"){
            			sourceType = ""
            		}else{
            			tourType = formEnd.getValue("tourType");
            			if(!tourType){
                			Dialog.alert({
                				content:"入住类型不能为空！"
                			});
                			return;
                		}
            		}
            		
            		var noSignUpRoomPriceNum = 0;
            		
            		//拼接房型房价
            		for(var ri=0;ri<roomTypeFormData.length;ri++){
            			var	roomTypeForm = roomTypeFormData[ri],
            				signUpRoomPriceList = [],
            				selectPrice = roomTypeForm.selectPrice,
            				pkSignUpRoom = roomTypeForm.pkSignUpRoom,
            				version = roomTypeForm.version,
            				obj = {};
            			
            			for(var key in roomTypeForm){
            				var signUpRoomPrice = {};
            				if(key.indexOf("saleDate_"+selectPrice)>=0){
            					signUpRoomPrice = JSON.parse(roomTypeForm[key]);
            					signUpRoomPriceList.push(signUpRoomPrice);
            				}
            				if(key.indexOf("livePrice_"+selectPrice)>=0){
            					var tempKey = key.substring(("livePrice_"+selectPrice).length+1);
            					obj[tempKey] = obj[tempKey]?obj[tempKey]:{};
            					obj[tempKey].checkInPrice = roomTypeForm[key];
            				}
            				if(key.indexOf("threeMeals_"+selectPrice)>=0){
            					var tempKey = key.substring(("threeMeals_"+selectPrice).length+1);
            					obj[tempKey] = obj[tempKey]?obj[tempKey]:{};
            					if(roomTypeForm[key].length>0){
            						obj[tempKey].threeMeals = true;	
            					}else{
            						obj[tempKey].threeMeals = false;
            					}
            				}
            				if(key.indexOf("breakfast_"+selectPrice)>=0){
            					var tempKey = key.substring(("breakfast_"+selectPrice).length+1);
            					obj[tempKey] = obj[tempKey]?obj[tempKey]:{};
            					if(roomTypeForm[key].length>0){
            						obj[tempKey].breakfast = true;	
            					}else{
            						obj[tempKey].breakfast = false;
            					}
            				}
            			}
            			
            			for(var j=0;j<signUpRoomPriceList.length;j++){
            				var tobj = obj[signUpRoomPriceList[j].roomPrice];
            				$.extend(signUpRoomPriceList[j],tobj);
            				
            				if(!signUpRoomPriceList[j].checkInPrice){
            					noSignUpRoomPriceNum++;
            				}
            			}
            			
            			signUpRooms.push({
            				signUpRoom:{
            					priceType:selectPrice,
            					count: roomTypeForm.roomNum || roomTypeForm.realityNum,
            					payables: roomTypeForm.reservePrice,
            					pkSignUpRoom:pkSignUpRoom,
            					roomType: roomTypeForm.pkRoomType,
                				checkInPriceMark: roomTypeForm.checkInPriceMark,
                				version:version
            				},
            				signUpRoomPriceList: signUpRoomPriceList
            			});
            		}
            		
            		if(noSignUpRoomPriceNum>0){
            			Dialog.alert({
            				content:"请填写入住价！"
            			});
            			return;
            		}
            		
            		if(!checkOutDate){
            			Dialog.alert({
            				content:"退房日期不能为空！"
            			});
            			return;
            		}
            		
            		//拼接房间信息
            		var roomPersonalInfosData = [];
            		var roomCheckIn = false;
            		var roomCheckInNum = 0;
            		var roomSelectAlertNum = 0;
            		for(var i=0; i<editgridData.length;i++){
            			var roomObj = {},
            			editgridDataItems = editgridData[i];
            			
            			if(editgridDataItems.nonMemberRegister.signUpRegister.personalInfo.sex){
            				editgridDataItems.nonMemberRegister.signUpRegister.personalInfo.sex = editgridDataItems.nonMemberRegister.signUpRegister.personalInfo.sex.key;
            			}else{
            				editgridDataItems.nonMemberRegister.signUpRegister.personalInfo.sex = "Male";
            			}
            			
            			if(editgridDataItems.checkIn){
            				if(editgridDataItems.checkIn.constructor == Boolean){
            					roomObj.checkIn = editgridDataItems.checkIn;
            				}else{
            					roomObj.checkIn = editgridDataItems.checkIn.key            					
            				}
            			}else{
            				roomObj.checkIn = false;
            			};
            			
            			if(roomObj.checkIn.constructor == Boolean){
            				if(roomObj.checkIn){
            					roomCheckIn = true;
            					roomCheckInNum++;
            				}
            			}else{
            				if(roomObj.checkIn == "true"){
            					roomCheckIn = true;
            					roomCheckInNum++;
            				} 
            			}
            			
            			
            			roomObj.pkRoomPersonalInfo = editgridDataItems.pkRoomPersonalInfo;
            			roomObj.version = editgridDataItems.version;
            			roomObj.nonMemberRegister = editgridDataItems.nonMemberRegister;
            			
            			if(roomObj.nonMemberRegister.signUpRegister.personalInfo.pkPersonalInfo){
            				roomObj.nonMemberRegister.signUpRegister.personalInfo = roomObj.nonMemberRegister.signUpRegister.personalInfo.pkPersonalInfo;
            			}
            			if(editgridDataItems.nonMemberRegister.signUpRegister.travelContract){
            				roomObj.nonMemberRegister.signUpRegister.travelContract = editgridDataItems.nonMemberRegister.signUpRegister.travelContract.pkTravelContract;
            			}
            			
            			var roomRegister = {
            					checkInPrice:editgridDataItems.roomRegister.checkInPrice,
            					breakfast:editgridDataItems.roomRegister.breakfast,
            					threeMeals:editgridDataItems.roomRegister.threeMeals,
            					pkRoomRegister:editgridDataItems.roomRegister.pkRoomRegister,
            					version:editgridDataItems.roomRegister.version
            			};
            			var version = editgridDataItems.roomRegister.version;
            			if(version){
            				roomRegister.version = version
            			}else{
            				roomRegister.version = 0
            			}
            			var room = editgridDataItems.roomRegister.room;
            			if(room){
            				roomRegister.room = room.pkRoom
            				roomSelectAlertNum++;
                		}else{
                			roomRegister.room = ""
                		}
            			
            			roomObj.roomRegister = roomRegister;
            			roomPersonalInfosData.push(roomObj);
            		}
            		
            		if(roomSelectAlertNum<editgridData.length){
            			Dialog.alert({
            				content:"请选择房间！"
            			});
            			return;
            		}
            		
            		if(roomCheckInNum == editgridData.length){
            			roomCheckIn=true;
            		}
            		
            		checkInData.roomPersonalInfoList = roomPersonalInfosData;
            		checkInData.signUpRoomViewList = signUpRooms;
            		checkInData.pkCIImplement = form.getValue("pkCIImplement");
            		checkInData.version = form.getValue("version");            		
            		checkInData.servicePoint = form.getValue("pkServicePoint");
            		checkInData.dataSource = form.getValue("dataSource");
            		checkInData.statementStatus = statementStatus?statementStatus:"NoStatement";
            		checkInData.roomStatus = roomStatus?roomStatus:"Pended";
            		checkInData.status = status?status:"Confirmed";
            		checkInData.nonMemGroupReserve = {
            				version:versionNonMemGroupReserve,
            				pkNonMemGroupReserve:pkNonMemGroupReserve,
            				nonMemberGroup:{
            					checkInDate:checkInDate,
            					checkOutDate:checkOutDate,
            					operator:operator,
            					operateDate:operateDate,
            					sourceType:sourceType,
            					tourType:tourType,
            					signUpType:signUpType,
            					pkNonMemberGroup:pkNonMemberGroup,
            					servicePoint:form.getValue("pkServicePoint"),
            					version:versionNonMemberGroup,
            					status:statusNonMemberGroup?statusNonMemberGroup:"CheckIn",
            					pledge:Number(pledge).toFixed(2),
            					chargeDeposit:false,
            					isAbolished:false
            				},
            		};
            		
            		if(roomCheckIn){
            			checkInData.nonMemGroupReserve.nonMemberGroup.status = "CheckIn";
            		}
            		aw.saveOrUpdate("api/nonmembergroup/checkinsave",this.customParam(checkInData),function(data){
            			widget.toGrid();
            			widget.queryByStatus(widget);
        			});
            },
            customParam:function(jsonObject,parentKey){
        		var ret="";
        		if(typeof jsonObject === "object" && jsonObject.constructor === Array){
        			for(var a=0;a<jsonObject.length;a++){
        				ret+=jsonObject[a].name+"="+jsonObject[a].value+"&";
        			}
        			return ret;
        		}
        		if(typeof jsonObject === "object"){
        			for(var i in jsonObject){
        				var value=jsonObject[i];
        				if(value===0){
        					value="0";
        				}
        				if(value === false){
        					value="false";
        				}
        				if(value && typeof value=='object' && value.constructor==Array){
        					//数组
        					var temp=parentKey ? parentKey+"."+i : i;
        					for(var j=0;j<value.length;j++){
        						if(typeof value[j] === "object"){
        							ret+=this.customParam(value[j], temp+"["+j+"]");
        						}else{
        							ret+=this.customParam(value[j],temp);
        						}
        					}
        				}else if(value && typeof value=='object'){
        					//对象
        					var temp=parentKey ? parentKey+"."+i : i;
        					ret+=this.customParam(value,temp);
        				}else if(value){
        					//普通类型
        					var temp=parentKey ? parentKey+"."+i : i;
        					ret+=temp+"="+value+"&";
        				}else{
        					var temp=parentKey ? parentKey+"."+i : i;
        					ret+=temp+"="+""+"&";
        				}
        			}
        			return ret;
        		}else if(!jsonObject && !parentKey){
        			return "";
        		}else{
        			return parentKey+"="+jsonObject+"&";
        		}
        	},

            //上门登记
            handleVisitClick: function(widget){
            	widget.get("editgrid").setData([]);
            	widget.get("editgrid").refresh();
        		widget.toCheckInDoor(null);
        		var subnav = widget.get("subnav"),
        		doorForm = widget.get("doorForm"),
        			servicePoint = subnav.getText("servicePoint"),
        			pkServicePoint = subnav.getValue("servicePoint");        		
        		doorForm.setValue("servicePoint",servicePoint);
        		doorForm.setValue("pkServicePoint",pkServicePoint);
        		doorForm.setValue("sourceType","DropIn");
        		doorForm.setValue("signUpType","NonMember");
        		$(".J-formEnd",widget.element).empty();
        		ScatterCheckInFormEnd.initComponent(widget);
            }
        },
        grid: {
        	//预约登记、修改、续住
            handleAppointmentClick: function (widget, sdata) {
            	var data = processData.cloneObj(sdata);
            	var form = null;
            	
            	if(widget.get("showForm")=='.J-form'){
        			form = widget.get("form");
        		}else{
        			form =widget.get("doorForm");
        		}
            	
            	var editgrid = widget.get("editgrid"),
            	 	servicePoint = data.servicePoint.name,
        			pkServicePoint = data.servicePoint.pkServicePoint,
        			checkInDate = data.nonMemGroupReserve.nonMemberGroup.checkInDate,
        			checkOutDate = data.nonMemGroupReserve.nonMemberGroup.checkOutDate,
        			pkNonMemberGroup = data.nonMemGroupReserve.nonMemberGroup.pkNonMemberGroup,
        			versionNonMemberGroup = data.nonMemGroupReserve.nonMemberGroup.version,
        			statusNonMemberGroup = data.nonMemGroupReserve.nonMemberGroup.status.key,
        			contactName = data.nonMemGroupReserve.nonMemberGroup.contactName,
        			contactPhone = data.nonMemGroupReserve.nonMemberGroup.contactPhone,
        			signUpNum = data.nonMemGroupReserve.nonMemberGroup.signUpNum,
        			description=data.nonMemGroupReserve.nonMemberGroup.description,
        			signUpRooms = data.nonMemGroupReserve.signUpRooms,
        			//count = data.nonMemGroupReserve.signUpRooms,
        			chargeDeposit = data.nonMemGroupReserve.nonMemberGroup.chargeDeposit
        			deposit = data.nonMemGroupReserve.nonMemberGroup.deposit
        			
        			money = data.nonMemGroupReserve.nonMemberGroup.money,
        			roomTypes = data.nonMemGroupReserve.signUpRooms,
        			dataSource = data.dataSource.key,
//        			sourceType,
//        			signUpType,
        			version = data.version,
        			pkCIImplement = data.pkCIImplement,
        			pkNonMemGroupReserve = data.nonMemGroupReserve.pkNonMemGroupReserve,
        			versionNonMemGroupReserve = data.nonMemGroupReserve.version,
        			status = data.status.key,
        			roomStatus = data.roomStatus,
        			statementStatus = data.statementStatus;
            		arr = [];
            	if(data.nonMemGroupReserve.nonMemberGroup.sourceType){
            		sourceType = data.nonMemGroupReserve.nonMemberGroup.sourceType.key;
            	}else{
            		sourceType = ""
            	}
            	if(data.nonMemGroupReserve.nonMemberGroup.signUpType){
            		signUpType = data.nonMemGroupReserve.nonMemberGroup.signUpType.key;
            	}else{
            		signUpType = ""
            	}
        		for(var k=0;k<roomTypes.length;k++){
        			arr.push(roomTypes[k].roomType.pkRoomType);
        		}
        		var str = "";
        		for(var i=0;i<signUpRooms.length;i++){
        			if(i==0){
        				str = signUpRooms[i].roomType.name + "(" + signUpRooms[i].count + ")";
        			}else{
        				str = str + "," +signUpRooms[i].roomType.name + "(" + signUpRooms[i].count + ")";
        			}
        		}
        		
        		var chargeDepositValue = "";
        		if(chargeDeposit){
        			chargeDepositValue = "是";
        		}else{
        			chargeDepositValue = "否";
        		}
        		
        		form.setValue("servicePoint",servicePoint);
        		form.setValue("pkServicePoint",pkServicePoint);
        		form.setValue("checkInDate",checkInDate);
        		form.setValue("checkOutDate",checkOutDate);
        		
        		if(widget.get("showForm")=='.J-form'){
        			form.setValue("contactName",contactName);   
            		form.setValue("contactPhone",contactPhone);  
            		form.setValue("deposit",deposit);            
            		form.setValue("chargeDepositValue",chargeDepositValue);   
            		form.setValue("roomTypeStr",str);    
            		form.setValue("signUpNum",signUpNum);     
        		}
        		
        		
        		form.setValue("description",description);
        		form.setValue("sourceType",sourceType);
        		form.setValue("signUpType",signUpType);
        		form.setValue("version",version);
        		form.setValue("pkCIImplement",pkCIImplement);
        		form.setValue("dataSource",dataSource);
        		form.setValue("pkNonMemGroupReserve",pkNonMemGroupReserve);
        		form.setValue("versionNonMemGroupReserve",versionNonMemGroupReserve);
        		form.setValue("pkNonMemberGroup",pkNonMemberGroup);
        		form.setValue("versionNonMemberGroup",versionNonMemberGroup);
        		form.setValue("statusNonMemberGroup",statusNonMemberGroup);
        		form.setValue("status",status);
        		form.setValue("statementStatus",statementStatus);
        		form.setValue("roomStatus",roomStatus);
        		var temp = [];
        		var pkOrgBuildingIn = [];
        		for(var i in data.roomPersonalInfos){
        			var roomPersonalInfos ={};
        			if(!data.roomPersonalInfos[i].nonMemberRegister.signUpRegister.exitGroup){
        				roomPersonalInfos = data.roomPersonalInfos[i];
        				
        				if(roomPersonalInfos.roomRegister.room){
        					pkOrgBuildingIn.push(roomPersonalInfos.roomRegister.room.orgBuilding.pkOrgBuilding);	
        				}
        				temp.push(roomPersonalInfos);
        			}
        		}
        		editgrid.setData(temp);
        		
        		ScatterCheckInFormEnd.initComponent(widget);
        		var formEnd = widget.get("formEnd");
        		var nonMemberType = scatterCheckInBiz.nonMemberTypeS(data,widget);
        		if(formEnd.$("input[name=money]").length>0){
        			formEnd.setValue("money",money);
        		}
        		if(data.nonMemGroupReserve.nonMemberGroup.tourType){
        			formEnd.setValue("tourType",data.nonMemGroupReserve.nonMemberGroup.tourType.key);
        		}
				var operator = data.nonMemGroupReserve.nonMemberGroup.operator.name;
        		var pkOperator = data.nonMemGroupReserve.nonMemberGroup.operator.pkOperator;
        		var operateDate = data.nonMemGroupReserve.nonMemberGroup.operateDate;
        		if(operator){
        			formEnd.setValue("operator",operator);
        		}
        		if(pkOperator){
        			formEnd.setValue("pkOperator",pkOperator);
        		}
        		if(operateDate){
        			formEnd.setValue("operateDate",operateDate);
        		}
        		var ipledge = data.nonMemGroupReserve.nonMemberGroup.pledge;
        		ipledge = Number(ipledge).toFixed(2);
        		formEnd.setValue("pledge",ipledge);        		
        		
        		this.queryRoomTypePrice(widget,checkInDate,checkOutDate,arr.join(","),roomTypes,nonMemberType,pkOrgBuildingIn.join(","));
        		
        		// 预约显示三种类型散客团，体验显示一种散客团，禁用下拉选择框
        		if(form.getValue("pkCIImplement") && 
        				data.nonMemGroupReserve.nonMemberGroup.signUpType &&
        				data.nonMemGroupReserve.nonMemberGroup.signUpType.key == "Experience"){
        			formEnd.setValue("signUpType",data.nonMemGroupReserve.nonMemberGroup.signUpType.key);
        			formEnd.setDisabled("signUpType",true);
        		}else if(form.getValue("pkCIImplement") && 
        				!data.nonMemGroupReserve.nonMemberGroup.signUpType){
        			formEnd.setDisabled("tourType",true);
        		}
            },
            queryRoomTypePrice:function(widget,checkInDate,checkOutDate,pkRoomTypeIn,roomTypes,nonMemberType,pkOrgBuildingIn){
            	var cParams = {
        			"saleDate":checkInDate,
        			"saleDateEnd":checkOutDate,
        			"roomType.pkRoomTypeIn": pkRoomTypeIn,
        			"orgBuilding.pkOrgBuildingIn":pkOrgBuildingIn,
        			fetchProperties:"orgBuilding.pkOrgBuilding,standardPrice,vipPrice,protocolPrice,pkRoomTypePrice,roomType.*,saleDate"
        		};
        		
        		scatterCheckInSvc.queryRoomTypePrice(cParams,function(ret){
        			var roomPriceData = processData.roomPriceData(widget,ret);
    				ScatterCheckInRoomForm.renderForm(widget,nonMemberType,roomPriceData,roomTypes);
    				//暂不显示早餐和三餐
    				$(".el-checkradio").hide();
        		});
            },
        },
        form: {
        	handleSelectRoomClick: function (widget) {
        		
            }
        }
    };
});